﻿Imports System.Drawing

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ComponentModel.DefaultEvent("AddImage_Click")> _
Public Class EdPicureBox
    Inherits System.Windows.Forms.PictureBox

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)
        Me.ResizeRedraw = True

    End Sub

#Region "Designer Generated Code"
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private WithEvents components As System.Windows.Forms.PictureBox

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.Windows.Forms.PictureBox()
        Me.SuspendLayout()
        '
        'components
        '
        Me.components.Location = New System.Drawing.Point(0, 0)
        Me.components.Name = "EdPicureBox"
        Me.components.Size = New System.Drawing.Size(100, 23)
        Me.components.TabIndex = 0
        '
        'PictureBox
        '
        Me.Name = "EdPicureBox"
        Me.ResumeLayout(False)
    End Sub
#End Region

#Region "Declarations"
    Dim iBorder As Boolean = True
    Dim iBorderDarkColor As Color = SystemColors.ActiveBorder
    Dim iBorderLightColor As Color = SystemColors.InactiveBorder

    Dim addRect As Rectangle
    Dim addFocus As Boolean = False
    'Dim editRect As Rectangle
    'Dim editFocus As Boolean = False
    Dim delRect As Rectangle
    Dim delFocus As Boolean = False
    Dim iPictureNotSetMessage As String = "Picture has not yet been set, user the add button bellow to add a picture"

    Public Event AddImage_Click(sender As Object, e As EventArgs)
    'Public Event EditImage_Click(sender As Object, e As EventArgs)
    Public Event DeleteImage_Click(sender As Object, e As EventArgs)

#End Region

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        'MyBase.OnPaint(e)

        Dim gh As New GraphicsHelper

        Dim bordersRect As New Rectangle(Me.ClientRectangle.X, _
                                         Me.ClientRectangle.Y, _
                                         Me.ClientRectangle.Width - 1, _
                                         Me.ClientRectangle.Height - 1)
        'Draw borders
        If DesignMode = False Then
            If Me.iBorder Then
                e.Graphics.DrawPath(New Pen(Me.iBorderDarkColor), _
                                    gh.GetRoundCornerPathForRectangle(New Rectangle(bordersRect.X + 1, _
                                                                                    bordersRect.Y + 1, _
                                                                                    bordersRect.Width - 2, _
                                                                                    bordersRect.Height - 2)))
                e.Graphics.DrawPath(New Pen(Me.iBorderLightColor), _
                        gh.GetRoundCornerPathForRectangle(New Rectangle(bordersRect.X, _
                                                                        bordersRect.Y, _
                                                                        bordersRect.Width - 2, _
                                                                        bordersRect.Height - 2)))
            End If
        End If

        'Get actual draw area
        Dim ab As Rectangle = Nothing
        If iBorder Then
            ab = New Rectangle(Me.ClientRectangle.X + 3, _
                                    Me.ClientRectangle.Y + 3, _
                                    Me.ClientRectangle.Width - 7, _
                                    Me.ClientRectangle.Height - 7)
        End If

        'Draw image
        If Me.Image Is Nothing Then
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            e.Graphics.DrawString(Me.iPictureNotSetMessage, MyBase.Font, Brushes.Black, ab, sf)
        Else
            'GetImageRectangleProportionalCentered1
            Dim imgRect As Rectangle = gh.GetImageRectangleProportionalCentered(Me.Image, ab)
            e.Graphics.DrawImage(Me.Image, imgRect)
        End If

        'Buttons area
        Dim bRect As New Rectangle(ab.Right - 36, ab.Bottom - 20, 54, 18)
        ''buttons area background
        'Dim bbcol As Color = Color.FromArgb(127, Color.White)
        'e.Graphics.FillRectangle(New SolidBrush(bbcol), bRect)

        'Add button
        Me.addRect = New Rectangle(bRect.X + 1, bRect.Y + 1, 16, 16)
        If addFocus Then
            e.Graphics.DrawImage(My.Resources.Icon_Add_HighlightYellow, Me.addRect)
        Else
            e.Graphics.DrawImage(My.Resources.Icon_Add_Gray, Me.addRect)
        End If

        'delete button
        Me.delRect = New Rectangle(bRect.X + 18, bRect.Y + 1, 16, 16)
        If delFocus Then
            e.Graphics.DrawImage(My.Resources.Icon_Delete_HighlightYellow, Me.delRect)
        Else
            e.Graphics.DrawImage(My.Resources.Icon_Delete_Gray, Me.delRect)
        End If


    End Sub

    Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)

        'add rec
        If Me.addRect.Contains(e.Location) Then
            If Me.addFocus = False Then
                Me.addFocus = True
                Me.Invalidate()
            End If
        Else
            If Me.addFocus = True Then
                Me.addFocus = False
                Me.Invalidate()
            End If
        End If

        'del rec
        If Me.delRect.Contains(e.Location) Then
            If Me.delFocus = False Then
                Me.delFocus = True
                Me.Invalidate()
            End If
        Else
            If Me.delFocus = True Then
                Me.delFocus = False
                Me.Invalidate()
            End If
        End If

    End Sub

    Protected Overrides Sub OnMouseUp(e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)

        'add rec
        If Me.addRect.Contains(e.Location) Then
            RaiseEvent AddImage_Click(Me, New EventArgs)
        End If

        'del rec
        If Me.delRect.Contains(e.Location) Then
            Me.Image = Nothing
            Me.Invalidate()
            RaiseEvent DeleteImage_Click(Me, New EventArgs)
        End If

    End Sub


End Class
